package com.atguigu.gulimall.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gulimall.order.test.User;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;


@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues:声明是需要监听的所有队列
     *
     * 参数可以写一下内容
     * 1、Message message:原生消息的详细信息,头+体
     * 2、T<发送消息的类型> User use
     * 3、Channel channel:当前传输数据的通道
     *
     * 队列Queue:可以有很多人监听,只要收到消息,队列删除消息,只能有一个人收到此消息的场景:
     * 1)、订单服务启动多个,同一个消息,只能有一个客户端端收到
     * 2)、只有一个消息完全处理完,方法运行结束才能接收到下一个消息
     *
     *
     */
    @RabbitListener(queues = {"hello-java-queue"})
    public void recieveMessage(Message message,
                               User user,
                               Channel channel) throws IOException {
        //消息体的内容
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties messageProperties = message.getMessageProperties();
        System.out.println("接收到的消息..."+message+"====>内容是:"+user);
        //channel内按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //签收货物,非批量模式
        channel.basicAck(deliveryTag,false);
        //requeue true的话返回队列,false的话直接丢弃
        channel.basicNack(deliveryTag,false,false);
    }

}